package com.ourteam.product.requirement.dao.impl;

import com.ourteam.product.requirement.dao.IProductTeamDAO;
import com.ourteam.product.requirement.dao.ProductTeam;
import com.ourteam.product.requirement.dao.ProductTeamDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-05-03 16:45:40
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductTeamDAOJdbcImpl extends AbstractSpringDAO
    implements IProductTeamDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ProductDefineId, StafferId, StafferRole, Status, Remarks,
            StafferName, StafferPhoto, ProductName
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ProductDefineId,
            _DataObjectProperty.StafferId, _DataObjectProperty.StafferRole,
            _DataObjectProperty.Status, _DataObjectProperty.Remarks,
            _DataObjectProperty.StafferName, _DataObjectProperty.StafferPhoto,
            _DataObjectProperty.ProductName
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductTeam, ProductTeamDAOQueryBean> daoHelper;

    public AbstractProductTeamDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductTeam, ProductTeamDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.system.dao.ISysUserDAO.EntityName)
                                .append(" SYS_USER ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.system.dao.ISysUserDAO.Id,
                    com.ourteam.system.dao.ISysUserDAO.EntityName + ".",
                    "SYS_USER."));
            relatedJoinSQLBuffer.append(" = ").append(StafferId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.requirement.dao.IProductDefineDAO.EntityName)
                                .append(" PRODUCT_DEFINE ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.requirement.dao.IProductDefineDAO.Id,
                    com.ourteam.product.requirement.dao.IProductDefineDAO.EntityName +
                    ".", "PRODUCT_DEFINE."));
            relatedJoinSQLBuffer.append(" = ").append(ProductDefineId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductTeam
         * @throws Exception
         */
    public boolean checkIsUnique(ProductTeam aProductTeam)
        throws Exception {
        if (aProductTeam == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductTeam.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductTeam
     * @throws Exception
     */
    public void insert(ProductTeam aProductTeam) throws Exception {
        super.doInsert(aProductTeam);
    }

    /**
         * Batch Insert Datas
         * @param aProductTeam
         * @throws Exception
         */
    public void batchInsert(ProductTeam[] aProductTeams)
        throws Exception {
        super.doBatchInsert(aProductTeams);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductTeamDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductTeam aProductTeam) throws Exception {
        delete(aProductTeam.getId());
    }

    /**
     * Update Data Selective
     * @param aProductTeam
     * @throws Exception
     */
    public void update(ProductTeam aProductTeam) throws Exception {
        if (aProductTeam.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductTeam);

        ProductTeam tempObj = findById(aProductTeam.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductTeam, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductTeams[]
     * @throws Exception
     */
    public void batchUpdate(ProductTeam[] aProductTeams)
        throws Exception {
        super.doBatchUpdate(aProductTeams);
    }

    /**
         * Update Data By Condictions
         * @param aProductTeam
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductTeam aProductTeam,
        ProductTeamDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductTeam, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductTeamDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductTeam aProductTeam)
        throws Exception {
        if (aProductTeam.getStafferRole() == null) {
            throw new Exception(
                "The ProductTeam.stafferRole value is not allow NULL");
        }

        if (aProductTeam.getStatus() == null) {
            throw new Exception(
                "The ProductTeam.status value is not allow NULL");
        }

        if (aProductTeam.getRemarks() == null) {
            throw new Exception(
                "The ProductTeam.remarks value is not allow NULL");
        }

        if (aProductTeam.getStafferName() == null) {
            throw new Exception(
                "The ProductTeam.stafferName value is not allow NULL");
        }

        if (aProductTeam.getProductName() == null) {
            throw new Exception(
                "The ProductTeam.productName value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductTeam findById(final long id) throws Exception {
        ProductTeamDAOQueryBean queryBean = new ProductTeamDAOQueryBean();

        ProductTeamDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductTeam[] ProductTeams = query(queryBean);

        if (ProductTeams.length == 0) {
            return null;
        } else {
            return ProductTeams[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductTeam[]
         * @throws Exception
         */
    public ProductTeam[] query(ProductTeamDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcProductTeamRowMapper());

        return (ProductTeam[]) result.toArray(new ProductTeam[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductTeamDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductTeamDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductTeam[] aProductTeams,
        ProductTeamDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductTeams, queryBean);
    }

    public void synchronize(ProductTeam[] aProductTeams,
        ProductTeamDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductTeam> callBack)
        throws Exception {
        daoHelper.synchronize(aProductTeams, queryBean, callBack);
    }

    public static class JdbcProductTeamRowMapper implements RowMapper<ProductTeam> {
        public ProductTeam mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductTeam object = new ProductTeam();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object productDefineId = null;

                productDefineId = resultSet.getObject(columnIndex++);

                object.setProductDefineIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (productDefineId != null) {
                    setBeanValue(object, _DataObjectProperty.ProductDefineId,
                        productDefineId);
                }

                Object stafferId = null;

                stafferId = resultSet.getObject(columnIndex++);

                object.setStafferIdNull(resultSet.getBoolean(columnIndex++));

                if (stafferId != null) {
                    setBeanValue(object, _DataObjectProperty.StafferId,
                        stafferId);
                }

                Object stafferRole = null;

                stafferRole = resultSet.getObject(columnIndex++);

                object.setStafferRoleNull(resultSet.getBoolean(columnIndex++));

                if (stafferRole != null) {
                    setBeanValue(object, _DataObjectProperty.StafferRole,
                        stafferRole);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object stafferName = null;

                stafferName = resultSet.getObject(columnIndex++);

                object.setStafferNameNull(resultSet.getBoolean(columnIndex++));

                if (stafferName != null) {
                    setBeanValue(object, _DataObjectProperty.StafferName,
                        stafferName);
                }

                Object stafferPhoto = null;

                stafferPhoto = resultSet.getObject(columnIndex++);

                object.setStafferPhotoNull(resultSet.getBoolean(columnIndex++));

                if (stafferPhoto != null) {
                    setBeanValue(object, _DataObjectProperty.StafferPhoto,
                        stafferPhoto);
                }

                Object productName = null;

                productName = resultSet.getObject(columnIndex++);

                object.setProductNameNull(resultSet.getBoolean(columnIndex++));

                if (productName != null) {
                    setBeanValue(object, _DataObjectProperty.ProductName,
                        productName);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
